package com.yxxmg.spring.ai.sample.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Flux;

/**
 * @author : yxxmg
 * @version : 1.0
 * @description :
 * @since : 2025/3/21
 */
@RestController("/api")
public class ChatController {
    private final ChatClient chatClient;

    public ChatController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chat(@RequestParam("message") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return this.chatClient.prompt(prompt).stream().content();
    }

    @GetMapping(value = "/chat1", produces = MediaType.TEXT_PLAIN_VALUE)
    public String chat1(@RequestParam("message") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return this.chatClient.prompt(prompt).call().content();
    }
}
